package com.wsz.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.system.pojo.Role;
import com.wsz.system.pojo.UserRole;
import com.wsz.system.service.RoleService;
import com.wsz.system.mapper.RoleMapper;
import com.wsz.system.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wangs
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2022-11-07 21:02:33
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public Map<String, Object> getRolesByUserId(String id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id).select("role_id");
        List<UserRole> list = userRoleService.list(queryWrapper);
        List<String> roleId = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            roleId.add(list.get(i).getRoleId());
        }

        List<Role> allRoles = baseMapper.selectList(null);

        Map<String,Object> map = new HashMap<>();
        map.put("userRoleIds",roleId);
        map.put("allRoles",allRoles);
        return map;
    }

    @Override
    public void removeSysUserRole(String userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("user_id",userId);
        userRoleService.remove(queryWrapper);
    }

    @Override
    public void saveSysUserRole(List<UserRole> userRoleList) {
        userRoleService.saveBatch(userRoleList);
    }
}




